package com.lhj.es.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * @author linghaijin
 * @date 2021-09-26 13:18
 */
public class RSAUtil_PKCS8 {

    public static String PRI_KEY_PKCS8 = "-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCuq7yav5Kydedn\n" +
            "yNgYcVw0sGslNxvLlzIps71iytfuJKJbCTLKcqeNO7N0WMunxdIkHe7M0jqGwHFu\n" +
            "Wtag0dOnS4rLmFdVU1qEbGF+RbiCIgjx3w4lej9uD0P2WNJQ0+HuxRElPhnf3fVx\n" +
            "H3zy8ByH554cfPB+opqLe0UDgd6F5QoBe/4wmTM58PvpAB6cYCWEVQUnWOIfuv0l\n" +
            "pMTVqKYaIewmbbgHjw6s8EgYI+sw/5sKQ+/MWs609J72sWB9+5q63AXydCepVZ1T\n" +
            "BjtlaFGFCj2DDp4nyUhSxps5bvPANJIpjnPcQ32LTjYbeKKikanV0+E+NtHQMuHZ\n" +
            "XJ563zSvAgMBAAECggEAdFmzdYVc64F8VnbD82w3lmVZyd1up2wcnwi6g79InLpM\n" +
            "Z8W1zoMXNw3omPy5FBXCyAokUs1K7Q3twSvDQjbi/NJmLv5YVl8CsakcPCenIxAW\n" +
            "0z7YxoArFl4W0FcUKYEqQ4mDxUIbrDLG1sa5zX3CE4lmmeVjsYcQ2Q6QxyhQGPGA\n" +
            "5bjuIe+76jtH+Y0S63AVwmVVnoq1iPrQEcWi7+UxIo/2H9nf36UBWOOL/ZB+i6A2\n" +
            "6O+xPHw+9hmpVen2UK8jr2by2y53dvCh2Fv5L6YM3jwDqCKbfo/hF5m7jblT2uvb\n" +
            "A2aZTcriBXvU9qz+FWjEeRyhkRfZLZDdSQNVyZQZwQKBgQDkul6dU62VMvHSI8R1\n" +
            "USURI+YFeLVfMfskO2ZyDz09nuoYi3Ekkv7ZNqZKebYhIU80RLkRtLw921fPCdO0\n" +
            "OfG+0dQnE1fFh242yc1ljnMw9zi/tZIagt2Vzl5dBbQy/WO72q/pfUjUmuqoXUXs\n" +
            "GSfrZrHM/99dza/A05Dp3VQa4QKBgQDDf1djNBwJ3pXsd6xhJOKPFNsQPkkJ5+yb\n" +
            "shKGjvGe+/oXLGN23s+YOJZBPOStvhc1dAJs0a458+7g7A4OiJsBsJ0hLUW7zgi6\n" +
            "Ur65AH90L96IsQmsMyRb4JTExqsjoRq52Ql/SpOdrvybDrgbRlz0cBmf9XQZFuPS\n" +
            "9CoXI8NRjwKBgEX/IuVSrlRyer6sY3rjkkxkiAg0oIy5Zvn3k6pkL4GkGE/anI1s\n" +
            "zsD6kHlKNgp9fxen0FsyzEjBz2V0xGzrLqgO3rX2nW29vGTX7RufNHFPtvY0QroK\n" +
            "QnWN6iLdk//mu5bJnfv+c0NdBWLw6G0QZb8Mdm0eAOVcBOUl2bs4GH0BAoGATvpm\n" +
            "52Dp4uLVVo6SSTyJepxGU62V/DaB0/SAn3OoET5irnH0TNV+sFRL/1cGW1psLeAM\n" +
            "CsI6zJbLbnpuw1hUpr4e/IQ5o8F8rSk4e6zD0MB1+mrrKXIPwj5cW5KXGPHAxkv0\n" +
            "FkSelhlXW5z6q6ola2rOL10N5gTlTRfu2SJNiekCgYEAmqADo1cyuLsxaPCxOD3L\n" +
            "bb2BZjRB/J0zaD3Z2ogeAsOwO/pR+XtMn338PFTE/LOWvs8r+lz+97xV+gQxqFgn\n" +
            "+9BGq7d+w3F9ErhH9/W6lPTVfUAyqZkVHI4CCd50dNDr/zhxWmdyf3F12a7K602y\n" +
            "QhYd9g8dEvH08Td4bgWse4o=\n" +
            "-----END PRIVATE KEY-----";

    /**
     *
     * @param encryptRandomKey
     * @return private key
     * @throws Exception
     */
    public static String getPrivateKey(String encryptRandomKey) throws Exception {
        String privKeyPEMnew = PRI_KEY_PKCS8.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
        byte[] decoded = Base64.decodeBase64(privKeyPEMnew);
        PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        // 64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(encryptRandomKey.getBytes(StandardCharsets.UTF_8));

        // RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);

        return new String(cipher.doFinal(inputByte));
    }

}
